<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Go语言包和包管理 | anyang的博客</title>
    <meta name="description" content="千里之行,始于足下">
    <meta name="generator" content="VuePress 1.3.1">
    <link rel="icon" href="/img/favicon.ico">
  <script charset="utf-8" src="/js/main.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
  <meta name="keywords" content="福小林,奔奔,ourLang,CentOS,JAVA,vue组件,lsdCloud">
  <script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-146891701-1"></script>
  <script>window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-146891701-1');</script>
  <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?56eae8eec590ccaef1d5ff99d766f315";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    
    <link rel="preload" href="/assets/css/0.styles.9fff9873.css" as="style"><link rel="preload" href="/assets/js/app.1bc80adb.js" as="script"><link rel="preload" href="/assets/js/2.7c0608ab.js" as="script"><link rel="preload" href="/assets/js/97.c5a6a5a1.js" as="script"><link rel="prefetch" href="/assets/js/10.81caa2ab.js"><link rel="prefetch" href="/assets/js/100.05bb269d.js"><link rel="prefetch" href="/assets/js/101.3e18b4d6.js"><link rel="prefetch" href="/assets/js/102.c7d7ed9c.js"><link rel="prefetch" href="/assets/js/103.4c771ce7.js"><link rel="prefetch" href="/assets/js/104.a2acd367.js"><link rel="prefetch" href="/assets/js/105.63399350.js"><link rel="prefetch" href="/assets/js/106.8a4744cc.js"><link rel="prefetch" href="/assets/js/107.7739d796.js"><link rel="prefetch" href="/assets/js/108.8008ae33.js"><link rel="prefetch" href="/assets/js/109.01c63898.js"><link rel="prefetch" href="/assets/js/11.7b837560.js"><link rel="prefetch" href="/assets/js/110.ad01aca9.js"><link rel="prefetch" href="/assets/js/111.3d74db70.js"><link rel="prefetch" href="/assets/js/112.67f55c34.js"><link rel="prefetch" href="/assets/js/113.016cd3c0.js"><link rel="prefetch" href="/assets/js/114.860e4b2b.js"><link rel="prefetch" href="/assets/js/115.b2f26258.js"><link rel="prefetch" href="/assets/js/116.8bea67b4.js"><link rel="prefetch" href="/assets/js/117.20682843.js"><link rel="prefetch" href="/assets/js/118.86a22e16.js"><link rel="prefetch" href="/assets/js/119.ad518bea.js"><link rel="prefetch" href="/assets/js/12.8fd78714.js"><link rel="prefetch" href="/assets/js/120.f97dd86f.js"><link rel="prefetch" href="/assets/js/121.c5601b4a.js"><link rel="prefetch" href="/assets/js/122.2889645a.js"><link rel="prefetch" href="/assets/js/13.ebf26820.js"><link rel="prefetch" href="/assets/js/14.b92dca09.js"><link rel="prefetch" href="/assets/js/15.b3dcef45.js"><link rel="prefetch" href="/assets/js/16.50378990.js"><link rel="prefetch" href="/assets/js/17.056bba2b.js"><link rel="prefetch" href="/assets/js/18.9047bb38.js"><link rel="prefetch" href="/assets/js/19.ab53b0fe.js"><link rel="prefetch" href="/assets/js/20.fe0b8af0.js"><link rel="prefetch" href="/assets/js/21.20a702f3.js"><link rel="prefetch" href="/assets/js/22.0ee08d29.js"><link rel="prefetch" href="/assets/js/23.02039a9a.js"><link rel="prefetch" href="/assets/js/24.363d3d74.js"><link rel="prefetch" href="/assets/js/25.dd6b101e.js"><link rel="prefetch" href="/assets/js/26.20493714.js"><link rel="prefetch" href="/assets/js/27.708723e5.js"><link rel="prefetch" href="/assets/js/28.4561834a.js"><link rel="prefetch" href="/assets/js/29.15cfcf5f.js"><link rel="prefetch" href="/assets/js/3.8d39d40c.js"><link rel="prefetch" href="/assets/js/30.04ab69de.js"><link rel="prefetch" href="/assets/js/31.b11c4302.js"><link rel="prefetch" href="/assets/js/32.81ae74ee.js"><link rel="prefetch" href="/assets/js/33.ddf09cc0.js"><link rel="prefetch" href="/assets/js/34.bc3549f7.js"><link rel="prefetch" href="/assets/js/35.4a0e0ebd.js"><link rel="prefetch" href="/assets/js/36.961a1c50.js"><link rel="prefetch" href="/assets/js/37.d8658de5.js"><link rel="prefetch" href="/assets/js/38.f76063aa.js"><link rel="prefetch" href="/assets/js/39.73edd08f.js"><link rel="prefetch" href="/assets/js/4.916af9bf.js"><link rel="prefetch" href="/assets/js/40.72314bff.js"><link rel="prefetch" href="/assets/js/41.c5eb1147.js"><link rel="prefetch" href="/assets/js/42.07aeafbb.js"><link rel="prefetch" href="/assets/js/43.c4a652ba.js"><link rel="prefetch" href="/assets/js/44.91fbe22b.js"><link rel="prefetch" href="/assets/js/45.6eecf0c4.js"><link rel="prefetch" href="/assets/js/46.81b7f108.js"><link rel="prefetch" href="/assets/js/47.b4468ab3.js"><link rel="prefetch" href="/assets/js/48.df205f23.js"><link rel="prefetch" href="/assets/js/49.7df59fa3.js"><link rel="prefetch" href="/assets/js/5.4fcd540a.js"><link rel="prefetch" href="/assets/js/50.0dc2fc5b.js"><link rel="prefetch" href="/assets/js/51.1ffabe54.js"><link rel="prefetch" href="/assets/js/52.2269d284.js"><link rel="prefetch" href="/assets/js/53.81b23843.js"><link rel="prefetch" href="/assets/js/54.b3249def.js"><link rel="prefetch" href="/assets/js/55.50dcf2af.js"><link rel="prefetch" href="/assets/js/56.c399c272.js"><link rel="prefetch" href="/assets/js/57.024f1dbb.js"><link rel="prefetch" href="/assets/js/58.680c6cf4.js"><link rel="prefetch" href="/assets/js/59.2291e9d7.js"><link rel="prefetch" href="/assets/js/6.7610d1d4.js"><link rel="prefetch" href="/assets/js/60.e59a26c1.js"><link rel="prefetch" href="/assets/js/61.ff5c64e4.js"><link rel="prefetch" href="/assets/js/62.7d97062c.js"><link rel="prefetch" href="/assets/js/63.6456dc7e.js"><link rel="prefetch" href="/assets/js/64.0b055680.js"><link rel="prefetch" href="/assets/js/65.9f25601c.js"><link rel="prefetch" href="/assets/js/66.cd973084.js"><link rel="prefetch" href="/assets/js/67.9b8f9716.js"><link rel="prefetch" href="/assets/js/68.d6c1869a.js"><link rel="prefetch" href="/assets/js/69.0a3688dd.js"><link rel="prefetch" href="/assets/js/7.9ebbd956.js"><link rel="prefetch" href="/assets/js/70.22f48891.js"><link rel="prefetch" href="/assets/js/71.9eb4fda9.js"><link rel="prefetch" href="/assets/js/72.ca4fa80c.js"><link rel="prefetch" href="/assets/js/73.17d1ab37.js"><link rel="prefetch" href="/assets/js/74.139691db.js"><link rel="prefetch" href="/assets/js/75.08d0560f.js"><link rel="prefetch" href="/assets/js/76.61078c10.js"><link rel="prefetch" href="/assets/js/77.4482adcc.js"><link rel="prefetch" href="/assets/js/78.166f2c37.js"><link rel="prefetch" href="/assets/js/79.2c7d6b79.js"><link rel="prefetch" href="/assets/js/8.6f8916c2.js"><link rel="prefetch" href="/assets/js/80.80445c3f.js"><link rel="prefetch" href="/assets/js/81.14e3bae8.js"><link rel="prefetch" href="/assets/js/82.7dac687f.js"><link rel="prefetch" href="/assets/js/83.1dd2e78e.js"><link rel="prefetch" href="/assets/js/84.23b759de.js"><link rel="prefetch" href="/assets/js/85.7574f69f.js"><link rel="prefetch" href="/assets/js/86.102dac61.js"><link rel="prefetch" href="/assets/js/87.8f5d639e.js"><link rel="prefetch" href="/assets/js/88.e564cbbe.js"><link rel="prefetch" href="/assets/js/89.cd66eb78.js"><link rel="prefetch" href="/assets/js/9.baa162db.js"><link rel="prefetch" href="/assets/js/90.685c6878.js"><link rel="prefetch" href="/assets/js/91.b8d1b7ea.js"><link rel="prefetch" href="/assets/js/92.1faa374d.js"><link rel="prefetch" href="/assets/js/93.cf5ff33c.js"><link rel="prefetch" href="/assets/js/94.8a02aefb.js"><link rel="prefetch" href="/assets/js/95.1b379a5b.js"><link rel="prefetch" href="/assets/js/96.9fd67185.js"><link rel="prefetch" href="/assets/js/98.9a231411.js"><link rel="prefetch" href="/assets/js/99.5b344cab.js">
    <link rel="stylesheet" href="/assets/css/0.styles.9fff9873.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">anyang的博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/go/introduction.html" class="sidebar-link">Go语言</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go教程</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go Web框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Go工具中间件</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go/middleware/go-coding-standard.html" class="sidebar-link">Golang语言编码规范</a></li><li><a href="/go/middleware/go-get命令详解.html" class="sidebar-link">go get命令详解</a></li><li><a href="/go/middleware/go-mysql.html" class="sidebar-link">Go语言操作Mysql</a></li><li><a href="/go/middleware/go-redis.html" class="sidebar-link">go-redis文档</a></li><li><a href="/go/middleware/go-swagger.html" class="sidebar-link">go-swagger安装及使用</a></li><li><a href="/go/middleware/go-viper.html" class="sidebar-link">viper操作文档</a></li><li><a href="/go/middleware/Goland的自定义快捷键配置.html" class="sidebar-link">配置Goland的自定义快捷键</a></li><li><a href="/go/middleware/Golang的安装配置golint.html" class="sidebar-link">Golang的安装配置golint</a></li><li><a href="/go/middleware/goproxy.html" class="sidebar-link">GoLand配置GOPROXY代理</a></li><li><a href="/go/middleware/gorm.html" class="sidebar-link">GORM基本操作</a></li><li><a href="/go/middleware/json-iterator.html" class="sidebar-link">json-iterator使用</a></li><li><a href="/go/middleware/package.html" class="active sidebar-link">Go语言包和包管理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_1-包简介" class="sidebar-link">1 包简介</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_1-1-工作空间" class="sidebar-link">1.1 工作空间</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_1-2-源文件" class="sidebar-link">1.2 源文件</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_1-3-包命名" class="sidebar-link">1.3 包命名</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_1-4-main-包" class="sidebar-link">1.4 main 包</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_2导包" class="sidebar-link">2导包</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_2-1-两种方式" class="sidebar-link">2.1 两种方式</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_2-2-包的别名" class="sidebar-link">2.2 包的别名</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_2-3-简洁模式" class="sidebar-link">2.3 简洁模式</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_2-4非导入模式-匿名导入" class="sidebar-link">2.4非导入模式(匿名导入)</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_2-5-导包的路径" class="sidebar-link">2.5 导包的路径</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_2-6-远程导入" class="sidebar-link">2.6 远程导入</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_3-初始化-init" class="sidebar-link">3 初始化 init</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_3-1-init总结" class="sidebar-link">3.1 init总结</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_4-包管理" class="sidebar-link">4 包管理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_4-1-演变过程" class="sidebar-link">4.1 演变过程</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_4-2-go-model优点" class="sidebar-link">4.2 Go Model优点</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_4-3-启用go-module" class="sidebar-link">4.3 启用go module</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_4-4-goproxy" class="sidebar-link">4.4 GOPROXY</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_5-go-mod详解" class="sidebar-link">5 go mod详解</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_5-1-go-mod命令" class="sidebar-link">5.1 go mod命令</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_5-2-go-mod说明" class="sidebar-link">5.2 go.mod说明</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_5-3-go-get" class="sidebar-link">5.3 go get</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_5-4-go-mod-edit" class="sidebar-link">5.4 go mod edit</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_6-项目中使用go-module" class="sidebar-link">6 项目中使用go module</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_6-1-现有项目" class="sidebar-link">6.1 现有项目</a></li><li class="sidebar-sub-header"><a href="/go/middleware/package.html#_6-2-新项目" class="sidebar-link">6.2 新项目</a></li></ul></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="go语言包和包管理"><a href="#go语言包和包管理" class="header-anchor">#</a> Go语言包和包管理</h1> <p><a data-fancybox="" title="goModel" href="/img/goImage/gomodel.png"><img src="/img/goImage/gomodel.png" alt="goModel"></a></p> <h2 id="_1-包简介"><a href="#_1-包简介" class="header-anchor">#</a> 1 包简介</h2> <h3 id="_1-1-工作空间"><a href="#_1-1-工作空间" class="header-anchor">#</a> 1.1 工作空间</h3> <blockquote><p>go语言的工作空间必须由 bin、pkg、src三个目录组成,可以在<code>GOPATH</code>环境变量中添加多个工作空间，但不能和<code>GOROOT</code>相同。通常<code>go get</code>使用第一个工作空间保存下载的第三方库</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code>workspace
	<span class="token operator">|</span>
	<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> bin <span class="token comment">// go install 安装目录。</span>
	<span class="token operator">|</span>	  <span class="token operator">|</span>
	<span class="token operator">|</span> 	  <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> learn
	<span class="token operator">|</span>
	<span class="token operator">|</span>
	<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> pkg <span class="token comment">// go build ⽣成静态库 (.a) 存放目录。</span>
	<span class="token operator">|</span> 	  <span class="token operator">|</span>
	<span class="token operator">|</span>	  <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> darwin_amd64
	<span class="token operator">|</span> 			<span class="token operator">|</span>
	<span class="token operator">|</span> 			<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> mylib<span class="token punctuation">.</span>a
	<span class="token operator">|</span> 					<span class="token operator">|</span>
	<span class="token operator">|</span> 					<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> mylib
	<span class="token operator">|</span>						 <span class="token operator">|</span>
	<span class="token operator">|</span>						 <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> sublib<span class="token punctuation">.</span>a
	<span class="token operator">|</span>
	<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> src <span class="token comment">// 项目源码目录。</span>
		  <span class="token operator">|</span>
		  <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> learn
		  <span class="token operator">|</span> 	 <span class="token operator">|</span>
		  <span class="token operator">|</span> 	 <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> main<span class="token punctuation">.</span><span class="token keyword">go</span>
		  <span class="token operator">|</span>
		  <span class="token operator">|</span>
		  <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> mylib
				 <span class="token operator">|</span>
				 <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> mylib<span class="token punctuation">.</span><span class="token keyword">go</span>
				 <span class="token operator">|</span>
				 <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> sublib
						<span class="token operator">|</span>
						<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">-</span> sublib<span class="token punctuation">.</span>g
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><h3 id="_1-2-源文件"><a href="#_1-2-源文件" class="header-anchor">#</a> 1.2 源文件</h3> <ul><li>编码：源码⽂件必须是 UTF-8 格式，否则会导致编译器出错。</li> <li>结束：语句以 “;” 结束，多数时候可以省略。</li> <li>注释：⽀持 “//”、 “/**/” 两种注释⽅式，不能嵌套。</li> <li>命名：采⽤ camelCasing ⻛格，不建议使⽤下划线。</li></ul> <h3 id="_1-3-包命名"><a href="#_1-3-包命名" class="header-anchor">#</a> 1.3 包命名</h3> <div class="custom-block tip"><p class="custom-block-title">包命名惯例</p> <ul><li>给包命名的惯例是使用包所在目录的名字。这让用户在导入包的时候，就能清晰地知道包名。</li> <li>尽量使用简介明了的名字，但要避免冲突</li> <li>包名一般使用单数的形式，但是避免冲突的用的复数比如bytes，errors，strings等</li> <li>要避免包名有其他含义。比如 temp这种</li> <li>命名时考虑包名和成员如何配合，尽量减少包名和成员有重复</li></ul></div> <h3 id="_1-4-main-包"><a href="#_1-4-main-包" class="header-anchor">#</a> 1.4 main 包</h3> <ul><li>所有用 Go 语言编译的可执行程序都必须有一个名叫 main 的包。 go语言的编译器会将这种名字的包编译为二进制可执行文件。</li> <li>main包下肯定会有名为main()的函数，main()是程序的入口。</li> <li>编译完会使用声明 main 包的代码所在的目录的目录名作为二进制可执行文件的文件名</li></ul> <h2 id="_2导包"><a href="#_2导包" class="header-anchor">#</a> 2导包</h2> <blockquote><p>关键字 Import ，进行导包。未使用的导入包，会编译错误。</p></blockquote> <h3 id="_2-1-两种方式"><a href="#_2-1-两种方式" class="header-anchor">#</a> 2.1 两种方式</h3> <blockquote><p>import a import b,…多次导入，以及import(a b c) 批量导入，如果导入的包不使用会报错。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token string">&quot;fmt&quot;</span>
<span class="token comment">//或者</span>
<span class="token keyword">import</span><span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
    <span class="token string">&quot;time&quot;</span>
<span class="token punctuation">)</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_2-2-包的别名"><a href="#_2-2-包的别名" class="header-anchor">#</a> 2.2 包的别名</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span><span class="token punctuation">(</span>
	io <span class="token string">&quot;fmt&quot;</span>
<span class="token punctuation">)</span>
io<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;hello world&quot;</span><span class="token punctuation">)</span> <span class="token comment">//别名可以直接用，在包重名时很有用</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="_2-3-简洁模式"><a href="#_2-3-简洁模式" class="header-anchor">#</a> 2.3 简洁模式</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token punctuation">.</span> <span class="token string">&quot;fmt&quot;</span>  <span class="token comment">//但是为了别人好看，一般还是不用这种</span>
<span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="_2-4非导入模式-匿名导入"><a href="#_2-4非导入模式-匿名导入" class="header-anchor">#</a> 2.4非导入模式(匿名导入)</h3> <blockquote><p>即导入一个包并不使用它。如果不加_,就会出现编译错误。 在这里用下划线 _ 重命名导入的包。只导入，不使用。
但是这个包它进行了初始化，一般在<code>init</code>函数调用，这样做的好处是，有些包我们不显示使用它，但是有可能用到它，或者由用户选择使用哪个。比如 对特定图像驱动包的初始化，在我们格式化转换图片用到。还有 <code>database/sql</code>包，可以先都初始化，让用户选择不同的数据库驱动。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token boolean">_</span> <span class="token string">&quot;test&quot;</span>     <span class="token comment">//非导入模式：仅让该包执行初始化函数</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_2-5-导包的路径"><a href="#_2-5-导包的路径" class="header-anchor">#</a> 2.5 导包的路径</h3> <blockquote><p>一般情况下是包的相对路径。比如<code>import &quot;learn/test&quot;</code>,标准库中的包会在安装 Go 的位置找到，即<code>GOROOT</code>。 Go 开发者创建的包会在 <code>GOPATH</code>环境变量指定的目录里查找。<code>GOPATH</code> 指定的这些目录就是开发者的个人工作空间。</p></blockquote> <h3 id="_2-6-远程导入"><a href="#_2-6-远程导入" class="header-anchor">#</a> 2.6 远程导入</h3> <blockquote><p>目前的大势所趋是，使用分布式版本控制系统<code>（Distributed Version Control Systems， DVCS）</code>
来分享代码，如<code>GitHub</code>、 <code>Launchpad</code> 还有 <code>Bitbucket</code>。 Go 语言的工具链本身就支持从这些网站及
类似网站获取源代码。 Go 工具链会使用导入路径确定需要获取的代码在网络的什么地方。 比如：<code>import &quot;github.com/xxxx/xxx&quot;</code>,用导入路径编译程序时，
go build 命令会使用 <code>GOPATH</code>的设置，在磁盘上搜索这个包。事实上，
这个导入路径代表一个URL，指向 <code>GitHub</code>上的代码库。如果路径包含 URL，可以使用 Go 工具链从
<code>DVCS</code> 获取包，并把包的源代码保存在<code>GOPATH</code>指向的路径里与 URL 匹配的目录里。这个获取过程
使用 go get 命令完成。 go get 将获取任意指定的 URL 的包，或者一个已经导入的包所依赖的其
他包。由于 go get的这种递归特性，这个命令会扫描某个包的源码树，获取能找到的所有依赖包。</p></blockquote> <h2 id="_3-初始化-init"><a href="#_3-初始化-init" class="header-anchor">#</a> 3 初始化 init</h2> <blockquote><p>每个包可以包含任意多个 init 函数，这些函数都会在程序执行开始的时候被调用。所有被编译器发现的 init 函数都会安排在 main 函数之前执行。
init 函数用在设置包、初始化变量或者其他要在程序运行前优先完成的引导工作。举例如下</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code> <span class="token keyword">package</span> postgres

 <span class="token keyword">import</span> <span class="token punctuation">(</span>
	 <span class="token string">&quot;database/sql&quot;</span>
 <span class="token punctuation">)</span>
 <span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//初始化函数</span>
     <span class="token comment">//这里省略。。</span>
	 sql<span class="token punctuation">.</span><span class="token function">Register</span><span class="token punctuation">(</span><span class="token string">&quot;postgres&quot;</span><span class="token punctuation">,</span> <span class="token function">new</span><span class="token punctuation">(</span>PostgresDriver<span class="token punctuation">)</span><span class="token punctuation">)</span>
 <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_3-1-init总结"><a href="#_3-1-init总结" class="header-anchor">#</a> 3.1 init总结</h3> <ul><li>每个源⽂件都可以定义⼀个或多个初始化函数。</li> <li>编译器不保证多个初始化函数执行次序。</li> <li>初始化函数在单⼀线程被调用，仅执行⼀次。</li> <li>初始化函数在包所有全局变量初始化后执行。</li> <li>在所有初始化函数结束后才执行 main.main。</li> <li>无法调用初始化函数。</li></ul> <h2 id="_4-包管理"><a href="#_4-包管理" class="header-anchor">#</a> 4 包管理</h2> <h3 id="_4-1-演变过程"><a href="#_4-1-演变过程" class="header-anchor">#</a> 4.1 演变过程</h3> <ul><li>2013年:Gedep,社区第一个包管理工具</li> <li>2015年:vendor,Golang官方(golang1.5)</li> <li>2016、2017 年:dep、manul、Godep、Govendor、godm （Govendor胜出）</li> <li>go module是Go1.11版本之后官方推出的版本管理工具，并且从Go1.13版本开始，go module将是Go语言默认的依赖管理工具。</li></ul> <h3 id="_4-2-go-model优点"><a href="#_4-2-go-model优点" class="header-anchor">#</a> 4.2 Go Model优点</h3> <ul><li>不必须将项目目录放在GOPATH中</li> <li>不使用vendor目录，而是统一安装到$GOPATH/pkg/mod/cache</li> <li>build/run时，自动析出项目import的包并安装</li></ul> <h3 id="_4-3-启用go-module"><a href="#_4-3-启用go-module" class="header-anchor">#</a> 4.3 启用go module</h3> <blockquote><p>要启用<code>go module</code>支持首先要设置环境变量<code>GO111MODULE</code>，通过它可以开启或关闭模块支持，它有三个可选值：off、on、auto，默认值是auto。
设置<code>GO111MODULE=on</code>之后就可以使用<code>go module</code>了，以后就没有必要在<code>GOPATH</code>中创建项目了，并且还能够很好的管理项目依赖的第三方包信息。
使用 <code>go module</code> 管理依赖后会在项目根目录下生成两个文件<code>go.mod</code>和<code>go.sum</code>。</p></blockquote> <ul><li><code>GO111MODULE=off</code>禁用模块支持,编译时会从<code>GOPATH</code>和<code>vendor</code>文件夹中查找包。</li> <li><code>GO111MODULE=on</code>启用模块支持,编译时会忽略<code>GOPATH</code>和<code>vendor</code>文件夹,只根据 <code>go.mod</code>下载依赖。</li> <li><code>GO111MODULE=auto</code>,当项目在<code>$GOPATH/src</code>外且项目根目录有<code>go.mod</code>文件时,开启模块支持。</li></ul> <h3 id="_4-4-goproxy"><a href="#_4-4-goproxy" class="header-anchor">#</a> 4.4 GOPROXY</h3> <blockquote><p>Go1.13之后<code>GOPROXY</code>默认值为<a href="https://proxy.golang.org" target="_blank" rel="noopener noreferrer">https://proxy.golang.org<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>，在国内是无法访问的，所以十分建议大家设置<code>GOPROXY</code>，详细设置请参考<a href="/go/learn-go/39 GoLand配置GOPROXY代理.html">设置GOPROXY教程</a></p></blockquote> <h2 id="_5-go-mod详解"><a href="#_5-go-mod详解" class="header-anchor">#</a> 5 go mod详解</h2> <h3 id="_5-1-go-mod命令"><a href="#_5-1-go-mod命令" class="header-anchor">#</a> 5.1 go mod命令</h3> <blockquote><p>常用的go mod命令如下：</p></blockquote> <table><thead><tr><th style="text-align:left;">go mod命令</th> <th>描述</th></tr></thead> <tbody><tr><td style="text-align:left;">go mod download</td> <td>下载依赖的module到本地cache（默认为$GOPATH/pkg/mod目录）</td></tr> <tr><td style="text-align:left;">go mod edit</td> <td>编辑go.mod文件</td></tr> <tr><td style="text-align:left;">go mod graph</td> <td>打印模块依赖图</td></tr> <tr><td style="text-align:left;">go mod init</td> <td>初始化当前文件夹, 创建go.mod文件</td></tr> <tr><td style="text-align:left;">go mod tidy</td> <td>增加缺少的module，删除无用的module</td></tr> <tr><td style="text-align:left;">go mod vendor</td> <td>将依赖复制到vendor下</td></tr> <tr><td style="text-align:left;">go mod verify</td> <td>校验依赖</td></tr> <tr><td style="text-align:left;">go mod why</td> <td>解释为什么需要依赖</td></tr></tbody></table> <h3 id="_5-2-go-mod说明"><a href="#_5-2-go-mod说明" class="header-anchor">#</a> 5.2 go.mod说明</h3> <p><code>go.mod</code>文件记录了项目所有的依赖信息，其结构大致如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>module github<span class="token punctuation">.</span>com<span class="token operator">/</span>ourlang<span class="token operator">/</span>studygo<span class="token operator">/</span>myblog

<span class="token keyword">go</span> <span class="token number">1.13</span>

require <span class="token punctuation">(</span>
	github<span class="token punctuation">.</span>com<span class="token operator">/</span>DeanThompson<span class="token operator">/</span>ginpprof v0<span class="token punctuation">.</span><span class="token number">0.0</span><span class="token operator">-</span><span class="token number">20190408063150</span><span class="token operator">-</span><span class="token number">3</span>be636683586
	github<span class="token punctuation">.</span>com<span class="token operator">/</span>gin<span class="token operator">-</span>gonic<span class="token operator">/</span>gin v1<span class="token punctuation">.</span><span class="token number">4.0</span>
	github<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token keyword">go</span><span class="token operator">-</span>sql<span class="token operator">-</span>driver<span class="token operator">/</span>mysql v1<span class="token punctuation">.</span><span class="token number">4.1</span>
	github<span class="token punctuation">.</span>com<span class="token operator">/</span>jmoiron<span class="token operator">/</span>sqlx v1<span class="token punctuation">.</span><span class="token number">2.0</span>
	github<span class="token punctuation">.</span>com<span class="token operator">/</span>satori<span class="token operator">/</span><span class="token keyword">go</span><span class="token punctuation">.</span>uuid v1<span class="token punctuation">.</span><span class="token number">2.0</span>
	google<span class="token punctuation">.</span>golang<span class="token punctuation">.</span>org<span class="token operator">/</span>appengine v1<span class="token punctuation">.</span><span class="token number">6.1</span> <span class="token comment">// indirect</span>
<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><ul><li>module用来定义包名</li> <li>require用来定义依赖包及版本</li> <li>indirect表示间接引用</li></ul> <h4 id="_5-2-1-依赖的版本"><a href="#_5-2-1-依赖的版本" class="header-anchor">#</a> 5.2.1 依赖的版本</h4> <blockquote><p><code>go mod</code>支持语义化版本号，比如<code>go get foo@v1.2.3</code>，也可以跟git的分支或tag，比如<code>go get foo@master</code>，当然也可以跟git提交哈希，比如<code>go get foo@e3702bed2</code>。关于依赖的版本支持以下几种格式：</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code>gopkg<span class="token punctuation">.</span>in<span class="token operator">/</span>tomb<span class="token punctuation">.</span>v1 v1<span class="token punctuation">.</span><span class="token number">0.0</span><span class="token operator">-</span><span class="token number">20141024135613</span><span class="token operator">-</span>dd632973f1e7
gopkg<span class="token punctuation">.</span>in<span class="token operator">/</span>vmihailenco<span class="token operator">/</span>msgpack<span class="token punctuation">.</span>v2 v2<span class="token punctuation">.</span><span class="token number">9.1</span>
gopkg<span class="token punctuation">.</span>in<span class="token operator">/</span>yaml<span class="token punctuation">.</span>v2 <span class="token operator">&lt;=</span>v2<span class="token punctuation">.</span><span class="token number">2.1</span>
github<span class="token punctuation">.</span>com<span class="token operator">/</span>tatsushid<span class="token operator">/</span><span class="token keyword">go</span><span class="token operator">-</span>fastping v0<span class="token punctuation">.</span><span class="token number">0.0</span><span class="token operator">-</span><span class="token number">20160109021039</span><span class="token operator">-</span>d7bb493dee3e
latest
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="_5-2-2-replace"><a href="#_5-2-2-replace" class="header-anchor">#</a> 5.2.2 replace</h4> <blockquote><p>在国内访问<code>golang.org/x</code>的各个包都需要翻墙，你可以在<code>go.mod</code>中使用<code>replace</code>替换成<code>github</code>上对应的库。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code>replace <span class="token punctuation">(</span>
	golang<span class="token punctuation">.</span>org<span class="token operator">/</span>x<span class="token operator">/</span>crypto v0<span class="token punctuation">.</span><span class="token number">0.0</span><span class="token operator">-</span><span class="token number">20180820150726</span><span class="token operator">-</span><span class="token number">614</span>d502a4dac <span class="token operator">=</span><span class="token operator">&gt;</span> github<span class="token punctuation">.</span>com<span class="token operator">/</span>golang<span class="token operator">/</span>crypto v0<span class="token punctuation">.</span><span class="token number">0.0</span><span class="token operator">-</span><span class="token number">20180820150726</span><span class="token operator">-</span><span class="token number">614</span>d502a4dac
	golang<span class="token punctuation">.</span>org<span class="token operator">/</span>x<span class="token operator">/</span>net v0<span class="token punctuation">.</span><span class="token number">0.0</span><span class="token operator">-</span><span class="token number">20180821023952</span><span class="token operator">-</span><span class="token number">922</span>f4815f713 <span class="token operator">=</span><span class="token operator">&gt;</span> github<span class="token punctuation">.</span>com<span class="token operator">/</span>golang<span class="token operator">/</span>net v0<span class="token punctuation">.</span><span class="token number">0.0</span><span class="token operator">-</span><span class="token number">20180826012351</span><span class="token operator">-</span><span class="token number">8</span>a410e7b638d
	golang<span class="token punctuation">.</span>org<span class="token operator">/</span>x<span class="token operator">/</span>text v0<span class="token punctuation">.</span><span class="token number">3.0</span> <span class="token operator">=</span><span class="token operator">&gt;</span> github<span class="token punctuation">.</span>com<span class="token operator">/</span>golang<span class="token operator">/</span>text v0<span class="token punctuation">.</span><span class="token number">3.0</span>
<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_5-3-go-get"><a href="#_5-3-go-get" class="header-anchor">#</a> 5.3 go get</h3> <div class="custom-block tip"><p class="custom-block-title">在项目中执行go get命令可以下载依赖包，并且还可以指定下载的版本。</p> <ul><li>运行<code>go get -u</code>将会升级到最新的次要版本或者修订版本(x.y.z, z是修订版本号， y是次要版本号)</li> <li>运行<code>go get -u=patch</code>将会升级到最新的修订版本</li> <li>运行<code>go get package@version</code>将会升级到指定的版本号<code>version</code></li> <li>如果下载所有依赖可以使用<code>go mod download</code>命令。</li></ul></div> <h4 id="_5-3-1-整理依赖"><a href="#_5-3-1-整理依赖" class="header-anchor">#</a> 5.3.1 整理依赖</h4> <blockquote><p>我们在代码中删除依赖代码后，相关的依赖库并不会在<code>go.mod</code>文件中自动移除。这种情况下我们可以使用<code>go mod tidy</code>命令更新<code>go.mod</code>中的依赖关系</p></blockquote> <h3 id="_5-4-go-mod-edit"><a href="#_5-4-go-mod-edit" class="header-anchor">#</a> 5.4 go mod edit</h3> <h4 id="_5-4-1-格式化"><a href="#_5-4-1-格式化" class="header-anchor">#</a> 5.4.1 格式化</h4> <blockquote><p>因为我们可以手动修改go.mod文件，所以有些时候需要格式化该文件。Go提供了一下命令：</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">go</span> mod edit <span class="token operator">-</span>fmt
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="_5-4-2-添加依赖项"><a href="#_5-4-2-添加依赖项" class="header-anchor">#</a> 5.4.2 添加依赖项</h4> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">go</span> mod edit <span class="token operator">-</span>require<span class="token operator">=</span>golang<span class="token punctuation">.</span>org<span class="token operator">/</span>x<span class="token operator">/</span>text
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="_5-4-3-移除依赖项"><a href="#_5-4-3-移除依赖项" class="header-anchor">#</a> 5.4.3 移除依赖项</h4> <blockquote><p>如果只是想修改<code>go.mod</code>文件中的内容，那么可以运行<code>go mod edit -droprequire=package path</code>，比如要在<code>go.mod</code>中移除<code>golang.org/x/text</code>包，可以使用如下命令</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">go</span> mod edit <span class="token operator">-</span>droprequire<span class="token operator">=</span>golang<span class="token punctuation">.</span>org<span class="token operator">/</span>x<span class="token operator">/</span>text
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_6-项目中使用go-module"><a href="#_6-项目中使用go-module" class="header-anchor">#</a> 6 项目中使用go module</h2> <h3 id="_6-1-现有项目"><a href="#_6-1-现有项目" class="header-anchor">#</a> 6.1 现有项目</h3> <p>如果需要对一个已经存在的项目启用<code>go module</code>，可以按照以下步骤操作：</p> <ul><li>在项目目录下执行<code>go mod init</code>，生成一个<code>go.mod</code>文件。</li> <li>执行<code>go get</code>，查找并记录当前项目的依赖，同时生成一个<code>go.sum</code>记录每个依赖库的版本和哈希值</li></ul> <h3 id="_6-2-新项目"><a href="#_6-2-新项目" class="header-anchor">#</a> 6.2 新项目</h3> <p>对于一个新创建的项目，我们可以在项目文件夹下按照以下步骤操作：</p> <ul><li>执行<code>go mod init</code> 项目名命令，在当前项目文件夹下创建一个<code>go.mod</code>文件。</li> <li>手动编辑<code>go.mod</code>中的<code>require</code>依赖项或执行<code>go get</code>自动发现、维护依赖。</li></ul></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/go/middleware/json-iterator.html" class="prev">
        json-iterator使用
      </a></span> <!----></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.1bc80adb.js" defer></script><script src="/assets/js/2.7c0608ab.js" defer></script><script src="/assets/js/97.c5a6a5a1.js" defer></script>
  </body>
</html>
